﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Alarm_Clock
{
    public partial class frmSetTime : Form
    {
        AlarmForm myParent;

        public frmSetTime(AlarmForm parent)
        {
            InitializeComponent();
            myParent = parent;
        }

        private void frmSetTime_Load(object sender, EventArgs e)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader("AlarmClock.properties");
            String s = "";
            while ((s = sr.ReadLine()) != null)
            {
                String key = s.Substring(0, s.IndexOf(":"));
                String value = s.Substring(s.IndexOf(":") + 1);
                switch (key)
                {
                    case "hour":
                        txtHour.Text = value;
                        break;
                    case "minute":
                        txtMinute.Text = value;
                        break;
                    case "offset":
                        txtOffset.Text = value;
                        break;
                    case "meridian":
                        if(value.Equals("AM"))
                        {
                            rdoAm.Checked = true;
                        }
                        else
                        {
                            rdoPm.Checked = true;
                        }
                        break;
                    case "playlistItem":
                        lstPlaylist.Items.Add(value);
                        break;
                }
            }
            sr.Close();
            txtHour.Focus();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter("AlarmClock.properties",false);
            sw.WriteLine("hour:" + txtHour.Text);
            sw.WriteLine("minute:" + txtMinute.Text);
            sw.WriteLine("offset:" + txtOffset.Text);
            //sw.WriteLine("meridian:" + (rdoAm.Checked ? "AM" : "PM"));
            for (int i = 0; i < lstPlaylist.Items.Count; i++)
            {
                sw.WriteLine("playlistItem:" + lstPlaylist.Items[i]);
            }
            sw.Close();
            myParent.reloadSettings();
            this.Dispose();
        }

        private void cmdAddSong_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            lstPlaylist.Items.Add(openFileDialog1.FileName);
        }

        private void cmdRemoveSong_Click(object sender, EventArgs e)
        {
            if (lstPlaylist.SelectedIndex >= 0)
            {
                lstPlaylist.Items.RemoveAt(lstPlaylist.SelectedIndex);
            }
        }

    }
}
